/*
 * Copyright (c) 2020 WildFireChat. All rights reserved.
 */

/*
 * Copyright (c) 2020 WildFireChat. All rights reserved.
 */

package cn.wildfire.chat.kit.conversation.ext;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;

import java.util.ArrayList;

import cn.wildfire.chat.kit.R;
import cn.wildfire.chat.kit.annotation.ExtContextMenuItem;
import cn.wildfire.chat.kit.contact.ContactListActivity;
import cn.wildfire.chat.kit.conversation.ext.core.ConversationExt;
import cn.wildfire.chat.kit.conversation.file.FileRecordListActivity;
import cn.wildfire.chat.kit.conversation.forward.ForwardPromptView;
import cn.wildfire.chat.kit.conversation.wallet.SendPackgeActivity;
import cn.wildfirechat.message.CardMessageContent;
import cn.wildfirechat.message.TextMessageContent;
import cn.wildfirechat.model.ChannelInfo;
import cn.wildfirechat.model.Conversation;
import cn.wildfirechat.model.GroupInfo;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;

public class RedPackgeExt extends ConversationExt {

    /**
     * @param containerView 扩展view的container
     * @param conversation
     */
    @ExtContextMenuItem
    public void pickContact(View containerView, Conversation conversation) {
        Intent intent = new Intent(fragment.getActivity(), SendPackgeActivity.class);
        if (conversation.type == Conversation.ConversationType.Single) {
            ArrayList<String> filterUserList = new ArrayList<>();
            filterUserList.add(conversation.target);
            intent.putExtra("targetId",conversation.target);
            intent.putExtra("isGroup",false);
            intent.putExtra(ContactListActivity.FILTER_USER_LIST, filterUserList);
        }
        if (conversation.type == Conversation.ConversationType.Group) {
            ArrayList<String> filterUserList = new ArrayList<>();
            filterUserList.add(conversation.target);
            intent.putExtra("targetId",conversation.target);
            intent.putExtra("isGroup",true);
            intent.putExtra(ContactListActivity.FILTER_USER_LIST, filterUserList);
        }
        startActivityForResult(intent, 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            TextMessageContent content = data.getParcelableExtra("content");
            if (content != null) {
                sendRedMessage(content);
            }
        }
    }

    /**
     */
    private void sendRedMessage(TextMessageContent content) {
//        ForwardPromptView view = new ForwardPromptView(fragment.getActivity());
//        if (conversation.type == Conversation.ConversationType.Single) {
//            UserInfo targetUser = ChatManager.Instance().getUserInfo(conversation.target, false);
//        } else if (conversation.type == Conversation.ConversationType.Group) {
//            GroupInfo groupInfo = ChatManager.Instance().getGroupInfo(conversation.target, false);
//        }
//        MaterialDialog dialog = new MaterialDialog.Builder(fragment.getActivity())
//            .customView(view, false)
//            .negativeText("取消")
//            .positiveText("发送")
//            .onPositive(new MaterialDialog.SingleButtonCallback() {
//                @Override
//                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    messageViewModel.sendMessage(conversation, content);
//                    if (!TextUtils.isEmpty(view.getEditText())) {
//                        TextMessageContent content = new TextMessageContent(view.getEditText());
//                        messageViewModel.sendMessage(conversation, content);
//                    }
//                    dialog.dismiss();
//                }
//            })
//            .build();
//        dialog.show();
    }

    @Override
    public int priority() {
        return 100;
    }

    @Override
    public int iconResId() {
        return R.mipmap.ic_send_redpackge;
    }

    @Override
    public String title(Context context) {
        return "红包";
    }

    @Override
    public String contextMenuTitle(Context context, String tag) {
        return title(context);
    }
}
